/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.riversoft.weixin.gateway;

import com.riversoft.weixin.gateway.beans.ResponseWrapper;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 *
 * @author johns
 */
public class WsdlFilter implements Filter {

    private static Logger logger = LoggerFactory.getLogger(WsdlFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String queryString = ((HttpServletRequest) servletRequest).getQueryString();
        if (queryString != null && queryString.toLowerCase().startsWith("wsdl")) {
            ResponseWrapper wrapperResponse = new ResponseWrapper((HttpServletResponse) servletResponse);//转换成代理类
            // 这里只拦截返回，直接让请求过去，如果在请求前有处理，可以在这里处理
            filterChain.doFilter(servletRequest, wrapperResponse);
            byte[] content = wrapperResponse.getContent();//获取返回值
            byte[] style = "<?xml-stylesheet type=\"text/xsl\" href=\"../wsdl-viewer.xsl\"?>".getBytes(Charset.forName("UTF-8"));
            byte[] start = "<?xml version='1.0' encoding='UTF-8' ?>".getBytes(Charset.forName("UTF-8"));
            int index = start.length - 1;
            byte[] target = new byte[content.length + style.length];
            byte[] body = Arrays.copyOfRange(content, index, content.length);
            servletResponse.getOutputStream().write(start);
            servletResponse.getOutputStream().write(style);
            servletResponse.getOutputStream().write(body);
        } else if(queryString != null && queryString.toLowerCase().startsWith("_wadl")) {
            ResponseWrapper wrapperResponse = new ResponseWrapper((HttpServletResponse) servletResponse);//转换成代理类
            // 这里只拦截返回，直接让请求过去，如果在请求前有处理，可以在这里处理
            filterChain.doFilter(servletRequest, wrapperResponse);
            byte[] content = wrapperResponse.getContent();//获取返回值
            byte[] style = "<?xml-stylesheet type=\"text/xsl\" href=\"../wadl-viewer.xsl\"?>".getBytes(Charset.forName("UTF-8"));
            byte[] start = "<?xml version='1.0' encoding='UTF-8' ?>".getBytes(Charset.forName("UTF-8"));
            int index = start.length - 1;
            byte[] target = new byte[content.length + style.length];
            //byte[] body = Arrays.copyOfRange(content, index, content.length);
            servletResponse.getOutputStream().write(start);
            servletResponse.getOutputStream().write(style);
            servletResponse.getOutputStream().write(content);
        }else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
